package General;

public class DistanceMatrix {

	private double mat[][]; 
	private int n;
	private AdjacencyMatrix a;
	
	public DistanceMatrix(AdjacencyMatrix a)
	{
		this.a=a;
		n=a.getSize();
		mat= new double [n][];
		int i,j;
		for (i=1; i<n; i++)
		{
			mat[i]=new double[i];
			for (j=0; j<i; j++)
				mat[i][j]=distance(i,j);
		}
	}
	private double distance (int u, int v)
	{
		byte[] vec1=a.getVector(u);
		byte[] vec2=a.getVector(v);
		int union=0, intersection=0;
		
		for (int i=0; i<n; i++)
		{
			if (vec1[i]==1 || vec2[i]==1)
				union++;
			if (vec1[i]==1 && vec2[i]==1)
				intersection++;
		}
		return 1-(((double)intersection)/union);
	}
	
	public double get(int i, int j)
	{
		if (i==j) return 0.0;
		if (i>j) return mat[i][j];
		return mat[j][i];
	}
	public void print()
	{
		System.out.println("DISTANCE MATRIX:");
		for (int i=0; i<n; i++)
		{
			System.out.println();
			for (int j=0; j<n; j++)
				System.out.printf("%4.4f ", get(i,j));
		}
	}
}
